public class SingList implements IList {
    //节点
    public static class ListNode {
        private int val;
        ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if (this.head == null) {
            this.head = newNode;
        } else {
            newNode.next = this.head;
            this.head = newNode;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if (this.head == null) {
            this.head = newNode;
        } else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = newNode;
        }
    }

    @Override
    public boolean addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            return false;
        }

        ListNode newNode = new ListNode(data);
        if(index == 0) {
            newNode.next = head;
            head = newNode;
            return true;
        }

        int count = 0;
        ListNode cur = this.head;
        while (count < index - 1) {
            cur = cur.next;
            count++;
        }
        newNode.next = cur.next;
        cur.next = newNode;
        return true;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head.val == key) {
            head = head.next;
        } else {
            ListNode cur = head.next;
            ListNode prev = head;
            while (cur.val != key) {
                prev = cur;
                cur = cur.next;
            }
            prev.next = cur.next;
            cur = null;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while(cur!=null) {
            if(cur.val==key) {
                prev.next = cur.next;
            }
            prev = cur;
            cur = cur.next;
        }
        if(this.head.val == key) {
            this.head = this.head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + "->");
            cur = cur.next;
        }
        System.out.println("null");
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur!=null) {
            ListNode curNext = cur.next;
            //cur.val= null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }
}
